package section4_11;

import java.util.concurrent.*;

/**
 * Created by Hyman on 2017/6/8.
 *
 * 在执行器中分离任务的启动与结果的处理
 */
public class Main {

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newCachedThreadPool();
        CompletionService<String> service = new ExecutorCompletionService<String>(executorService);

        // 创建两报表请求对象和两个线程来执行他们
        ReportRequest faceRequest = new ReportRequest("Face", service);
        ReportRequest onlineRequest = new ReportRequest("Online", service);
        Thread faceThread = new Thread(faceRequest);
        Thread onlineThread = new Thread(onlineRequest);

        // 创建一个报表处理对象和一个执行它的线程
        ReportProcessor processor = new ReportProcessor(service);
        Thread senderThread = new Thread(processor);

        // 启动线程
        System.out.printf("Main: Starting the Threads\n");
        faceThread.start();
        onlineThread.start();
        senderThread.start();

        // 等待报表生成器对象的任务完成
        try {
            System.out.printf("Main: Waiting for the report generators.\n");
            faceThread.join();
            onlineThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 关闭执行器
        System.out.printf("Main: Shuting down the executor.\n");
        executorService.shutdown();

        try {
            executorService.awaitTermination(1, TimeUnit.DAYS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        processor.setEnd(true);
        System.out.printf("Main: Ends\n");
    }

}
